package com.daily.pump.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.text.TextUtils;

import com.daily.pump.R;
import com.daily.pump.constants.ExtraConstants;
import com.daily.pump.widget.zxing.CameraPreview;
import com.daily.pump.widget.zxing.ScanView;
import com.xunxi.mimi.ui.base.BaseActivity;
import com.xunxi.mimi.utils.LogUtil;
import com.xunxi.mimi.utils.ToastUtil;

import butterknife.BindView;
import cn.bertsir.zbar.Qr.Symbol;
import cn.bertsir.zbar.QrConfig;
import cn.bertsir.zbar.ScanCallback;

public class ZxingScanActivity extends BaseActivity implements ScanCallback {

    @BindView(R.id.camera_preview)
    CameraPreview mCameraPreView;

    @BindView(R.id.scan_view)
    ScanView mZxingScanView;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState, String tag) {
        setContentView(R.layout.activity_zxing_scan);
    }

    @Override
    protected void initView() {
        mCameraPreView.setScanCallback(this);
    }

    @Override
    protected void initPresenter() {
        Symbol.scanType = QrConfig.TYPE_ALL;
    }

    @Override
    protected void initValue() {

    }

    @Override
    public void onScanResult(String content) {
        onScanQRCodeSuccess(content);
    }

    public void onScanQRCodeSuccess(String result) {
        try {
            vibrate();
            if (!TextUtils.isEmpty(result)){
                Intent bundle = new Intent();
                bundle.putExtra(ExtraConstants.DEVICE_NAME, result);
                setResult(RESULT_OK, bundle);
                finish();
                LogUtil.error(ZxingScanActivity.class, "onScanQRCodeSuccess:" + result);
            }else {
                reStartScan();
                ToastUtil.show(this, R.string.zxing_scan_fail);
            }
        } catch (Exception e) {
            //抛出异常重新开始扫码
            reStartScan();
            ToastUtil.show(this, R.string.zxing_scan_fail);
        }
    }

    /**
     * Desc:扫描成功震动
     * <p>
     * Author: [李豫]
     * Date: 2019-01-02
     * Update Time:
     * Update Comments:
     */
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    /**
     * Desc:重新开始扫码
     * <p>
     * Author: [李豫]
     * Date: 2019-01-11
     * Update Time:
     * Update Comments:
     */
    private void reStartScan() {
        if (mHandler != null) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mCameraPreView != null) {
                        mCameraPreView.start();
                    }
                    if (mZxingScanView != null) {//重新开始识别
                        mZxingScanView.onResume();
                    }
                }
            }, 2000);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mCameraPreView != null) {
            mCameraPreView.start();
        }
        if (mZxingScanView != null) {
            mZxingScanView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mCameraPreView != null) {
            mCameraPreView.stop();
        }
        if (mZxingScanView != null) {
            mZxingScanView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mCameraPreView != null) {
            mCameraPreView.setFlash(false);
            mCameraPreView.stop();
        }
    }
}
